package com.epfresh.activity;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.constant.AppPurchaseRouterConstant;
import com.epfresh.api.entity.*;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.http.HttpRequest;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.universalimageloader.utils.Log;
import com.epfresh.api.utils.T;
import com.epfresh.bean.OrderMinusEntity;
import com.epfresh.constant.Constant;
import com.epfresh.global.BaseActivity;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

public class OrderMinusDetailActivity extends BaseActivity {

    static final String TAG = "OrderMinusDetailActivity";
    TextView tvState,tvMoneyTop, tvTime, tvReason,tvMoneyWay;
    View tvInfoHint;

    TextView tvStarter, tvType, tvMoneyBottom,tvNumber,tvMinusReason,tvId,tvApplyTime, tvInfo;
    View ivStarter;

    String id;
    private TextView mRefundChannelTv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        id = getIntent().getStringExtra("id");
        if(id!=null){
            requestOrderInfo();
        }else {
            T.toast("没收到订单号");
            finish();
        }
    }
    @TargetApi(Build.VERSION_CODES.M)
    void initView(){
        setContentView(R.layout.activity_order_minis_detail);
        View llSee = findViewById(R.id.ll_see);
        llSee.setOnClickListener(this);
        tvState = (TextView)findViewById(R.id.tv_state);
        tvMoneyTop = (TextView)findViewById(R.id.tv_money_top);
        tvTime = (TextView)findViewById(R.id.tv_time);
        tvReason = (TextView)findViewById(R.id.tv_reason);
        tvMoneyWay = (TextView)findViewById(R.id.tv_money_way);
        tvStarter = (TextView)findViewById(R.id.tv_starter);
        ivStarter = findViewById(R.id.iv_starter);
        tvType = (TextView)findViewById(R.id.tv_type);
        tvMoneyBottom = (TextView)findViewById(R.id.tv_money_bottom);
        tvNumber = (TextView)findViewById(R.id.tv_number);
        tvMinusReason = (TextView)findViewById(R.id.tv_minus_reason);
        tvId = (TextView)findViewById(R.id.tv_id);
        tvApplyTime = (TextView)findViewById(R.id.tv_apply_time);
        tvInfo = (TextView)findViewById(R.id.tv_info);
        ivBack.setVisibility(View.VISIBLE);
        tvInfoHint = findViewById(R.id.tv_info_hint);
        mRefundChannelTv = (TextView) findViewById(R.id.order_minis_detail_refund_channel_tv);
        tvReason.setOnClickListener(this);
        tvMoneyWay.setOnClickListener(this);
        ivStarter.setOnClickListener(this);
        rightbtn.setOnClickListener(this);
        rightbtn.setTextSize(15);
        rightbtn.setTextColor(Color.parseColor("#434343"));//getColor(R.color.order_minis_detail_refund_textcolor));
        rightbtn.setText("帮助");
        rightbtn.setVisibility(View.VISIBLE);
        toolbarTitle.setTextColor(Color.parseColor("#313131"));
        toolbarTitle.setTextSize(18);
        toolbarTitle.setText("减免退详情");
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.ll_see:
                Intent storeIntent = new Intent(mContext,
                        OrderMinusListActivity.class);
                storeIntent.putExtra("id", data.getOrderId());
                startActivity(storeIntent);
                break;
            case R.id.baseback:
                // 返回上一页
                finish();
                break;
            case R.id.tv_reason:
                Intent reason;
                reason = new Intent(this, FailReasonActivity.class);
                reason.putExtra("reason",data.getJmtApplyReasonDetails());
                reason.putExtra("phone",data.getJmtApplicantPhone());
                startActivity(reason);
                break;
            case R.id.rightbtn:
                Intent intent = new Intent(this, WebActivity.class);
                intent.putExtra(Constant.KEY_WEB_URL, com.epfresh.api.constant.Constant.URL_HELP_REDUCTION);
                intent.putExtra(Constant.KEY_WEB_TITLE, "帮助");
                startActivity(intent);
                break;
            case R.id.iv_starter:
                Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + data.getJmtApplicantPhone()));//Uri.parse("tel:" + "4000588198"));//调出来拨打页面；
                startActivity(call);
                break;
            case R.id.tv_money_way:
                Intent detail;
                detail = new Intent(this, OrderMinusMoneyInfoActivity.class);
                detail.putExtra("id",data.getId());
                startActivity(detail);
                break;
        }
    }


    OnRequestListener<OrderMinusEntity> onRequestListener = new OnRequestListener<OrderMinusEntity>() {
        @Override
        public OrderMinusEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, OrderMinusEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            showExceptionView(R.mipmap.upload_error, "网络错误", HttpRequest.FAIL_NET_FAULT);
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            showExceptionView(R.mipmap.upload_error, arg + "", HttpRequest.FAIL_NET_FAULT);
        }
        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<OrderMinusEntity> response, Object tag) {
            OrderMinusEntity responseElement = response.getResponseElement();
            updateOrderInfo(responseElement);
            hideProgressDialog();
        }
    };

    public void requestOrderInfo() {
        RequestEntityPurchase<Map<String, String>> entity = new RequestEntityPurchase<>();
        entity.setCmd(AppPurchaseRouterConstant.cmd_order_jmt_detail);
        Map<String, String> map = new HashMap<>();
        map.put("jmtId",id);
        entity.setParameters(map);
        request(entity, AppPurchaseRouterConstant.cmd_order_jmt_detail, onRequestListener);
    }
    OrderMinusEntity data;

    public void updateOrderInfo(OrderMinusEntity data) {
        this.data = data;
        String jmtType = "退款";
        if(data.getJmtType()==2){
            jmtType = "减免";//1:退款；2:减免
            tvMoneyTop.setText(jmtType+"金额： "+data.getJmtTotalPrice()+"元");
/*            tvInfo.setVisibility(View.GONE);
            tvNumber.setVisibility(View.GONE);
            tvInfoHint.setVisibility(View.GONE);*/
        }else {
            tvMoneyTop.setText(jmtType+"金额： "+data.getRefundPrice()+"元");

/*            tvInfoHint.setVisibility(View.VISIBLE);
            tvInfo.setVisibility(View.VISIBLE);
            tvNumber.setVisibility(View.VISIBLE);*/
        }
        //1待退款,3退款中,red         5退款失败,7退款成功,9减免成功   black
    /*-----------------tvState--修改title的标题色----------by tony---------start------------------*/
        switch(data.getJmtRefundStatus()){
            case 1:
            case 3:
                tvState.setTextColor(getResources().getColor(R.color.font_color_dark));//黑的
                break;
            case 5:
            case 7:
            case 9:
                tvState.setTextColor(getResources().getColor(R.color.red_price));
                break;
        }
      /*-------------tvState------修改title的标题色----------by tony---------end------------------*/
        tvState.setText(data.getJmtRefundStatusTitle());

        if(data.getRefundDate()==null||data.getRefundDate().equals("")||data.getRefundDate().equals("null")) {
            tvTime.setVisibility(View.GONE);
            /*-----------------退款渠道和退款时间同时出现，同时消失-by tony---------start-------*/
            tvMoneyWay.setVisibility(View.GONE);

        }else{
            tvMoneyWay.setVisibility(View.VISIBLE);
            tvTime.setVisibility(View.VISIBLE);
            tvTime.setText(jmtType + "时间： " + data.getRefundDate());
        }
          /*----------------退款渠道和退款时间同时出现，同时消失-----by tony---------start-------*/
        tvStarter.setText("发起方：  "+data.getJmtApplicantName());
    //1,店铺；2，e批生鲜；3，配送中心；4，采购商
    if(data.getJmtOperatorType()==4){
            ivStarter.setVisibility(View.GONE);
        }else {
            ivStarter.setVisibility(View.VISIBLE);
        }
        tvType.setText("减免退类型："+data.getJmtSubTypeTitle());
        tvMoneyBottom.setText("减免退金额："+data.getJmtTotalPrice()+"元");
        if(data.getProductCountTitle()==null||data.getProductCountTitle().equals("")||data.getProductCountTitle().equals("null")){
            tvNumber.setVisibility(View.GONE);
        }else {
            tvNumber.setVisibility(View.VISIBLE);
            tvNumber.setText("退货数量：" + data.getProductCountTitle());
        }
        tvMinusReason.setText("减免退原因："+data.getJmtAppleReasonTypeTitle());
        tvId.setText("减免退编号："+data.getJmtNo());
        tvApplyTime.setText("申请时间："+data.getJmtApplyDate());
        if(data.getJmtApplyReasonDetails()==null||data.getJmtApplyReasonDetails().equals("")||data.getJmtApplyReasonDetails().equals("null")){
            tvInfoHint.setVisibility(View.GONE);
            tvInfo.setVisibility(View.GONE);
        }else {
            tvInfoHint.setVisibility(View.VISIBLE);
            tvInfo.setVisibility(View.VISIBLE);
        }
        tvInfo.setText(data.getJmtApplyReasonDetails());
        if (null!=data.getRefundPriceTitle()){
//            tvMoneyWay.setVisibility(View.VISIBLE);
            mRefundChannelTv.setVisibility(View.VISIBLE);
            mRefundChannelTv.setText("退款渠道："+data.getRefundPriceTitle());
        }else {
            tvMoneyWay.setVisibility(View.GONE);
            mRefundChannelTv.setVisibility(View.GONE);
        }
//1待退款,3退款中,red         5退款失败,7退款成功,9减免成功   black
        //
        switch(data.getJmtRefundStatus()){
            case 1:
            case 3:
               /* tvReason.setVisibility(View.GONE);
                tvMoneyWay.setVisibility(View.GONE);*/
                break;
            case 5:
                tvReason.setVisibility(View.GONE);
                break;
            case 7:
                tvMoneyWay.setVisibility(View.VISIBLE);
                break;
            case 9:
                break;
            default:
                tvReason.setVisibility(View.GONE);
                tvMoneyWay.setVisibility(View.GONE);
                break;

        }
    }
}
